package com.example.springboot.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.example.springboot.controller.dto.LoginDTO;
import com.example.springboot.controller.request.BaseRequest;
import com.example.springboot.controller.request.LoginRequest;
import com.example.springboot.entity.Slides;
import com.example.springboot.entity.Slides;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.SlidesMapper;
import com.example.springboot.service.ISlidesService;
import com.example.springboot.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class SlidesService implements ISlidesService {

    @Autowired //通过autowired导入slidesMapper
    SlidesMapper slidesMapper;

    public List<Slides> list(){
        return slidesMapper.list();
    }

    @Override
    public PageInfo<Slides> page(BaseRequest baseRequest){
//        dependency引入pagehelper
//        PageHelper传入页面和每页信息条数
        PageHelper.startPage(baseRequest.getPageNum(),baseRequest.getPageSize());
        List<Slides> slidess=slidesMapper.listByCondition(baseRequest);
        return new PageInfo<>(slidess);
    }

    @Override
    public void save(Slides slides){//        当做卡号来处理
        Date date= new Date();
//     通过hashCode编码将fastSimpleUUID产生的字母和数字转为数字
        try{
            slidesMapper.save(slides);
        }catch(DuplicateKeyException e){
            log.error("数据插入失败,title:{}",slides.getTitle());
            throw new ServiceException("用户插入失败");
        }
    }


    @Override
    public Slides getById(Integer id) {
        return slidesMapper.getById(id);
    }

    @Override
    public void update(Slides slides) {
        slides.setUpdateTime(new Date());
        slidesMapper.updateById(slides);

    }

    @Override
    public void deleteById(Integer id) {
        slidesMapper.deleteById(id);
    }
    
}
